﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using NBlog.Web.Application.Infrastructure;
using NBlog.Web.Application.Service;
using NBlog.Web.Application.Service.Entity;

namespace NBlog.Web.Controllers
{
    public partial class HomeController : LayoutController
    {
        public HomeController(IServices services) : base(services) { }

        [HttpGet]
        public ViewResult Index()
        {
            var model = new IndexModel();

            return View(model);
        }

        [HttpGet]
        public ViewResult About()
        {
            var model = new AboutModel();
            return View(model);
        }

        [HttpGet]
        public ViewResult Contact()
        {
            var model = new ContactModel();
            return View(model);
        }

        [HttpPost]
        public ViewResult Contact(ContactModel model)
        {
            return View(model);
        }

        [HttpGet]
        public ViewResult Advertise()
        {
            var model = new AdvertiseModel();
            return View(model);
        }

        [HttpGet]
        public ViewResult Privacy()
        {
            var model = new PrivacyModel();
            return View(model);
        }

        [HttpGet]
        public ViewResult Throw()
        {
            throw new NotImplementedException("Example exception for testing error handling.");
        }

        [HttpGet]
        public ActionResult WhoAmI()
        {
            return Content(User.Identity.Name.AsNullIfEmpty() ?? "Not logged in");
        }

        [HttpGet]
        public ActionResult WhatTimeIsIt()
        {
            return Content(DateTime.Now.ToString());
        }

        public ActionResult Reviews()
        {
            var brands = Services.Brand.GetList();
            var reviews = Services.Review.GetList();
            reviews.ForEach(r1 => r1.Brand = brands.Find(b => b.Slug.Equals(r1.BrandSlug)));

            var allReviews = new Dictionary<BrandEntity, List<ReviewEntity>>();
            foreach (ReviewEntity review in reviews)
            {
                if (!allReviews.ContainsKey(review.Brand)) allReviews.Add(review.Brand, new List<ReviewEntity>());

                allReviews[review.Brand].Add(review);
            }
            var model = new ReviewsModel();
            model.GrouppedReviews = allReviews;

            return View("Reviews", model);
        }
    }
}
